<?php

class DGuestEmail extends DActiveRecord {

    private $curPage = 0;
    private $defaultSort;
    public $startDate, $endDate;

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public function tableName() {
        return 'cf_guest_email';
    }

    public function rules() {
        return array(
			array('email', 'required'),
            array('email', 'email'),
            array('email', 'ext.validators.DupplicateValidator',
                'tableName'   => $this->tableName(),
                'isNewRecord' => $this->isNewRecord,
                'currId'      => $this->id,
            ),
			array('email, startDate, endDate', 'safe', 'on'=>'search'),
        );
    }

    public function relations() {
        return array();
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'name' => 'Email',
            'update_time' => 'Date',
        );
    }

    public function search() {
        //Restore state from session
        $this->restoreState();

        //Criteria
        $criteria = new CDbCriteria;
        $criteria->compare('email', $this->email, true);
        $criteria->compare('update_time', "<=$this->endDate");
        $criteria->compare('update_time', ">=$this->startDate");

        //Sorting
        $sort = new CSort();
        $sort->defaultOrder = ($this->defaultSort == '') ? array('id' => true) : $this->defaultSort;
        $sort->attributes = array(
            'id' => array(
                'asc'       => 'id ASC',
                'desc'      => 'id DESC',
                'default'   => 'desc',
            ),
            'email' => array(
                'asc'       => 'email ASC',
                'desc'      => 'email DESC',
                'default'   => 'desc',
            ),
            'update_time' => array(
                'asc'       => 'update_time ASC',
                'desc'      => 'update_time DESC',
                'default'   => 'desc',
            ),
        );
        $sort->applyOrder($criteria);

        return new CActiveDataProvider($this, array(
            'criteria'   => $criteria,
            'sort'       => $sort,
            'pagination' => array('currentPage' => $this->curPage),
        ));
    }

    private function restoreState() {
        $state = SpsState::getState();

        if ($state) {
            if (isset($state['sort']))
                $this->defaultSort = $state['sort'];
            if (isset($state['page']))
                $this->curPage     = $state['page'];

            if (isset($state['search'])) {
                foreach ($state['search'] as $key => $value)
                    if ($value != '')
                        $this->$key = $value;
            }
        }
    }
    
    public function export() {
        $tableName = $this->tableName();
        $sql = "
            SELECT email
            FROM $tableName
            ORDER BY update_time DESC
        ";
        $command = Yii::app()->db->createCommand($sql);
        $data = $command->queryColumn();
        
        return implode("\r\n", $data);
    }

}